/*
作者：江怀玉
日期：2015-06-10
功能：弹出编辑
解决：
-------------------------------------
依赖JS:
jquery,jquery.data
依赖CSS：

---------------------------------------------------------*/
//初始化控件
(function ($) {
    $.fn.EditWindow = function (options) {
        var _opt = !options ? {} : options;
         arguments
        var $this = this;
        $(this).show();
        var h = $(window).height();
        var w = $(window).width();
        $(this).height(h);
        $(this).width(w);
        fnCloseWindow();
        $(this).find(".main_data_window_content").height(h - 35);
        $(this).find(".main_data_window_content>iframe").height(h - 35 - 5);
        $(this).find("#main_data_window_footer").width(w);
        $(this).find("#main_data_window_footer").height(30);

        $(this).find(".main_data_window_footer>button").click(function () {
            switch ($(this).attr("id")) {
                case "btn_window_save":
                    fnWindowSaveData();
                    break;
                case "btn_window_save_and_return":
                    fnWindowSaveAndReturn();
                    break;
                case "btn_window_cancel":
                    fnCloseWindow();
                    break;
                case "btn_window_open":
                    fnOpenWindow(_EditDataURL);
                    break;
                case "btn_window_view":
                    fnOpenWindow(_EidtDataURL+"&read=true");
                    break;
                default:
                    break;
            }
        });
        //数据保存
        function fnWindowSaveData() {
            var editwindow = document.getElementById("main_data_edit_iframe");
            if (editwindow) {
                editwindow.contentWindow.Save();
            }
        }

        function fnWindowSaveAndReturn() {
            var editwindow = document.getElementById("main_data_edit_iframe");
            if (editwindow) {
                editwindow.contentWindow.Save(fnCloseWindow);
                //fnCloseWindow();
            }
        }
        //关闭窗口
        function fnCloseWindow() {
            $($this).animate({ left: (0 - $(window).width() - 5) + "px" });
            //$("#main_data_edit_iframe").attr("src", "");
        }
        //最小化窗口
        function fnMinWindow() {
            $($this).animate({ left: "-250px" });
        }

        //打开窗口
        function fnOpenWindow(url) {
            $("#main_data_edit_iframe").attr("src", url);
            $($this).animate({ left: "-2px" });
        }


        function fnViewWindow(url) {
            $("#main_data_edit_iframe").attr("src", url);
            $("#btn_window_save_and_return").hide();
            $($this).animate({ left: "-2px" });
        }


        //取消
        function fnCancel() {
            fnCloseWindow();
        }

        if (_opt.action == "open") {
            fnOpenWindow(_opt.url);
        }
    }
})(jQuery);


$(document).ready(function () {
    $("body").append("<div id='main_data_window' style='position:fixed;z-index:999;width:250px;background-color: #cccccc;border:solid 2px #ddd;top:-2px;left:-1500px;display:none;'>"
    //+ "<span class='main_data_window_title' style=\"color:#fff;\">标题</span>"
    //+ "<div class='main_data_window_footer' style=\"text-align:center;padding-right:5px;margin-top: -5px\"><div><button id=\"btn_window_open\" style=\"display:none;margin-right:2px;\" class=\"btn btn-info\">保存</button><button id=\"btn_window_save\" style=\"margin-right:2px;\" class=\"btn btn-info\">保存</button><button id=\"btn_window_cancel\" style=\"margin-right:2px;\" class=\"btn btn-info\">取消</button></div>"
    //+ "</div>"
    + "<div class='main_data_window_footer' style=\"text-align:right;padding-right:5px;margin-top: 3px;height:30px;\"><button id=\"btn_window_open\" style=\"display:none;\"></button><button id=\"btn_window_view\" style=\"display:none;\"></button><button id=\"btn_window_save_and_return\" style=\"margin-right:2px;\" class=\"btn btn-info\">保存并返回</button><button id=\"btn_window_save\" style=\"margin-right:2px;\" class=\"btn btn-info\">保存</button><button id=\"btn_window_cancel\" style=\"margin-right:2px;\" class=\"btn btn-info\">取消</button></div>"
    + "<div class='main_data_window_content' style='padding:3px;'>"
    + "<iframe id='main_data_edit_iframe' frameborder='0' src='' style='width: 100%; height: 99.5%;margin: 0px; padding: 0px;'></iframe>"
    + "</div>"
    + "</div>"
    );
    $("#main_data_window").EditWindow();
});


